package com.xkcoding.orm.jpa.repository;

import com.xkcoding.orm.jpa.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * <p>
 * User Dao
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-11-07 14:07
 */
@Repository
public interface UserDao extends JpaRepository<User, Long> {

    User findByName(String name);

    @Query(value = "select * from orm_user limit 1" , nativeQuery = true)
    User findOne();

    @Query(value = "select * from orm_user where if(?1 != '', LOCATE(?1, name)>0, 1=1) and if(?2 != '',status = ?2, 1=1) ",
        countQuery = "select * from orm_user where if(?1 != '', LOCATE(?1, name)>0, 1=1) and if(?2 != '',status = ?2, 1=1) ", nativeQuery = true)
    Page<User> queryPage(String name, Integer status, Pageable pageable);


    @Query(value = "select name,password from orm_user where if(?1 != '', LOCATE(?1, name)>0, 1=1) and if(?2 != '',status = ?2, 1=1) ",
        countQuery = "select name,password from orm_user where if(?1 != '', LOCATE(?1, name)>0, 1=1) and if(?2 != '',status = ?2, 1=1) ", nativeQuery = true)
    Page<Object> queryPageObject(String name, Integer status, Pageable pageable);

}
